package com.watchthelan.service.network;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;

import com.watchthelan.domain.Server;

public class SimpleSocketConnection implements SocketConnection {

	@Override
	public int test(String address, int port) throws SocketException {
		Socket sock = null;
		SocketAddress sockAddress = null;
		try {
			sock = new Socket();
			sockAddress = new InetSocketAddress(address, port);
			sock.connect(sockAddress);
			return STATE_RUN; // running
		} catch (SocketException e) {
			return STATE_STOP; // stopped
		} catch (Exception e) {
			return STATE_UNKNOWN; // unknown
		}	
	}

	@Override
	public int testServer(Server server) throws SocketException {
		if (server == null || server.getHostname() == null || server.getPort() == 0) {
			return STATE_STOP; //stopped
		}
		return this.test(server.getHostname(), server.getPort());
	}
}
